In [43]:
# necessary packages #

#using Pkg
#Pkg.add("Distances")
using Distributions
using Random
using Distances
using LinearAlgebra
using SparseArrays
using IterativeSolvers
using ProgressMeter
In [44]:
include("util.j")
Out[44]:
getAD (generic function with 1 method)
In [45]:
# unnecessary packages #

#using Pkg
#Pkg.add("UnicodePlots")
using UnicodePlots   # check the structure of the sparse matrix
using BenchmarkTools

using StatsPlots
using MCMCChains
In [46]:
#using Pkg
#Pkg.add("ProgressMeter");
In [47]:
# Set the parameters for SLMC model #

N = 1200 # sample size
N1 = 1000; N2 = 1000;
q = 2; p = 2; K = 3
Σ = [0.3 0.1
     0.1 0.2];
β = [1.0 -1.0
     -5.0 2.0];
ϕ1 = 3.0; ϕ2 = 16.0; ϕ3 = 30.0; ν1 = 0.5; ν2 = 0.5; ν3 = 0.5; # parameter for the independent F
Λ = [1.0 -2.0
     -2.0 3.0
     3.0 -4.0]; # loading matrix

# priors #
μβ = fill(0.0, p, q);  =[[100.0 0.0]; [0.0 100.0]];
μΛ = fill(0.0, K, q);  =[[100.0 0.0 0.0]; [0.0 100.0 0.0]; [0.0 0.0 100.0]];
νΣ = 2 * q; ΨΣ = [[1.0 0.0]; [0.0 1.0]];
In [48]:
# Generate simulation data #

Random.seed!(1234);
coords = rand(2, N);                                          # random location over unit square
X = hcat(fill(1, (N,)), rand(N));                             # design matrix
D = pairwise(Euclidean(), coords, dims = 2);                  # distance matrix
ρ1 = exp.(-ϕ1 * D); ρ2 = exp.(-ϕ2 * D);ρ3 = exp.(-ϕ3 * D);    # covariance matrix
ω = [rand(MvNormal(ρ1), 1) rand(MvNormal(ρ2), 1) rand(MvNormal(ρ3), 1)] * Λ; # latent process
Y = X * β + ω + transpose(rand(MvNormal(Σ), N));              # response matrix
In [49]:
# Some data preparations #

ordx = sortperm(coords[1, :]);                                # sort order based on the first coordinates
X_ord = X[ordx, :]; Y_ord = Y[ordx, :]; ω_ord = ω[ordx, :];   # sorted data
coords_ord = coords[:, ordx];
S1_ind = sample(1:N, N1, replace = false, ordered = true);    # observed location index for 1st response
S2_ind = sample(1:N, N2, replace = false, ordered = true);    # observed location index for 2nd response
S = sort(union(S1_ind, S2_ind));                              # observed index set
M1_ind = setdiff(S, S1_ind);                                  # in S not in S1
M2_ind = setdiff(S, S2_ind);                                  # in S not in S2 
obs_ind = vcat(S1_ind, S2_ind .+ N);              # index of the observed location for all response among N locations
perm_ind = sortperm(vcat(S1_ind, S2_ind));                    # the vector of the permutation 

v1 = zeros(N); v1[S1_ind] .= 1;
v2 = zeros(N); v2[S2_ind] .= 1;
index_S = (2^0 * v1 + 2^1 * v2);                              # build index indicating which response are observed
M1_Sind = findall(x -> x == 2^1, index_S[S]);                 # index of M1 among S
M2_Sind = findall(x -> x == 2^0, index_S[S]);                 # index of M2 among S

m = 10; n = length(S);                                        # number of nearest neighbor                       
NN = BuildNN(coords_ord[:, S], m, 1.0);                            # build nearest neighbor 
nnIndx_col = vcat(NN.nnIndx, 1:n);                            # the index of columns
nnIndx_row = zeros(Int64, 0);                                               
for i in 2:m
    nnIndx_row = vcat(nnIndx_row, fill(i, i-1))
end
nnIndx_row = vcat(nnIndx_row, repeat((m + 1):n, inner = m), 1:n);  # the index of rows

 = cholesky();  = cholesky();
In [50]:
# preallocation #

#F = Array{Float64,2}(undef, n , 3);                           # preallocate the matrix F
μ_m1 = Array{Float64, 2}(undef, length(M1_ind), q);
μ_m2 = Array{Float64, 2}(undef, length(M2_ind), q);
nIndx = length(NN.nnIndx);
A1 = Array{Float64}(undef, nIndx); D1 = Array{Float64}(undef, n);
A2 = Array{Float64}(undef, nIndx); D2 = Array{Float64}(undef, n);
A3 = Array{Float64}(undef, nIndx); D3 = Array{Float64}(undef, n);
Ystar = vcat(Y_ord[S, :], .L \ μβ, .L \ μΛ);             # will be updated after imputing missing response
Xstar = vcat([X_ord[S, :] fill(0.0, n, K)], [inv(.L) fill(0.0, p, K)], 
    [fill(0.0, K, p) inv(.L)]);      
Ψstar = fill(0.0, q, q); νstar = νΣ + n;
μγstar = vcat(μβ, μΛ); Vγstar = fill(0.0, p + K, p + K);
Y_Xm = fill(0.0, n, q); invVγstar = fill(0.0, p + K, p + K);

MCMC sampling algorithm Q1: priors for $\nu_i$ Q2: $\phi_i$ may not be consistant, since the order can change

In [51]:
# Preallocation for MCMC samples and Initalization #
N_sam = 25000;
γ_sam = Array{Float64, 3}(undef, p + K, q, N_sam + 1);
Σ_sam = Array{Float64, 3}(undef, q, q, N_sam + 1);
vF_sam = Array{Float64, 2}(undef, K * n, N_sam);
Y_m1_sam = Array{Float64, 2}(undef, length(M1_ind), N_sam);
Y_m2_sam = Array{Float64, 2}(undef, length(M2_ind), N_sam);

ϕ1_0 = ϕ1; ϕ2_0 = ϕ2; ϕ3_0 = ϕ3;
γ_sam[:, :, 1] = vcat(X \ Y, [[1.0 0.0]; [0.0 1.0]; [1.0 1.0]]);
Σ_sam[:, :, 1] = [[0.5 0.1]; [0.1 0.5]];

# first consider SLMC with fixed hyperparameter set {ψk} #
# Build the matrix Vk #
getAD(coords_ord[:, S], NN.nnIndx, NN.nnDist, NN.nnIndxLU, ϕ1_0, 0.5, A1, D1);
getAD(coords_ord[:, S], NN.nnIndx, NN.nnDist, NN.nnIndxLU, ϕ2_0, 0.5, A2, D2);
getAD(coords_ord[:, S], NN.nnIndx, NN.nnDist, NN.nnIndxLU, ϕ3_0, 0.5, A3, D3);
In [52]:
# for loop for MCMC chain #
prog = Progress(N_sam, 1, "Computing initial pass...", 50)
for l in 1:N_sam
    # Build the matrix D_Sigma_o^{1/2} #
    Dic_diag = Dict(2^0 => sparse(1I, 1, 1) * (1 / sqrt(Σ_sam[:, :, l][1, 1])), 
        2^1 => sparse(1I, 1, 1) * (1 / sqrt(Σ_sam[:, :, l][2, 2])), 
        (2^0 + 2^1)=> sparse(sqrt(inv(Σ_sam[:, :, l]))));
    invD = blockdiag([Dic_diag[i] for i in index_S if i > 0]...);
                    
    # first consider SLMC with fixed hyperparameter set {ψk} #
    # Build the matrix Vk #
    #getAD(coords_ord[:, S], NN.nnIndx, NN.nnDist, NN.nnIndxLU, ρ1_0, 0.5, A1, D1);
    #getAD(coords_ord[:, S], NN.nnIndx, NN.nnDist, NN.nnIndxLU, ρ2_0, 0.5, A2, D2);
    #getAD(coords_ord[:, S], NN.nnIndx, NN.nnDist, NN.nnIndxLU, ρ3_0, 0.5, A3, D3);

    # Build Ytilde Xtilde
    Ytilde = vcat(invD * vcat(Y_ord[S1_ind, 1], Y_ord[S2_ind, 2])[perm_ind], zeros(K * n));
    Xtilde = vcat(
             invD * kron(sparse(transpose(γ_sam[(p + 1):(p + K), :, l])), 
                            sparse(1:N, 1:N, ones(N)))[obs_ind, 
                            vcat(S, S .+ N, S .+ (2 * N))][perm_ind, :],
             blockdiag(
             Diagonal(1 ./ sqrt.(D1)) * sparse(nnIndx_row, nnIndx_col, vcat(-A1, ones(n))),
             Diagonal(1 ./ sqrt.(D2)) * sparse(nnIndx_row, nnIndx_col, vcat(-A2, ones(n))),
             Diagonal(1 ./ sqrt.(D3)) * sparse(nnIndx_row, nnIndx_col, vcat(-A3, ones(n)))))
                
    # use LSMR to generate sample of F #       
    nsam = length(Ytilde);
    vF_sam[:, l] = lsmr(Xtilde, Ytilde + rand(Normal(), nsam));
                    
    # impute missing response over S#
    Xstar[1:n, (p + 1):(p + K)] = reshape(vF_sam[:, l], :, 3);        # update matrix Xstar with F
    mul!(μ_m1, Xstar[M1_Sind, :], γ_sam[:, :, l]);
    mul!(μ_m2, Xstar[M2_Sind, :], γ_sam[:, :, l]);

    Y_m1_sam[:, l] = μ_m1[:, 1] + (Σ_sam[1, 2, l] / Σ_sam[2, 2, l]) * 
            (Y_ord[M1_ind, 2] - μ_m1[:, 2]) + 
            rand(Normal(0, sqrt(Σ_sam[1, 1, l] - Σ_sam[1, 2, l]^2 / Σ_sam[2, 2, l])), length(M1_ind));
    Y_m2_sam[:, l] = μ_m2[:, 2] + (Σ_sam[2, 1, l] / Σ_sam[1, 1, l]) * 
            (Y_ord[M2_ind, 1] - μ_m2[:, 1]) + 
            rand(Normal(0, sqrt(Σ_sam[2, 2, l] - Σ_sam[2, 1, l]^2 / Σ_sam[1, 1, l])), length(M2_ind)); # improve?...
                    
    # use MNIW to sample γ Σ #
    Ystar[M1_Sind, 1] = Y_m1_sam[:, l];              # update Ystar with imputed response
    Ystar[M2_Sind, 2] = Y_m2_sam[:, l]; 
    invVγstar = cholesky(Xstar'Xstar);
    mul!(μγstar, transpose(Xstar), Ystar); μγstar = invVγstar.U \ (invVγstar.L \ μγstar);
    Y_Xm = BLAS.gemm('N', 'N', -1.0, Xstar, μγstar) + Ystar;
    mul!(Ψstar, transpose(Y_Xm), Y_Xm); BLAS.axpy!(1.0, ΨΣ, Ψstar);

    Σ_sam[:, :, l + 1] = rand(InverseWishart(νstar, Ψstar), 1)[1];    # sample Σ
    γ_sam[:, :, l + 1] = (invVγstar.U \ reshape(rand(Normal(), (p + K) * q), (p + K), q)) * 
                    cholesky(Σ_sam[:, :, l + 1]).U + μγstar;          # sample γ  
    if γ_sam[p + 1, 1, l + 1] < 0 
        γ_sam[(p + 1), :, l + 1] = -γ_sam[(p + 1), :, l + 1]
    end
    if γ_sam[p + 2, 1, l + 1] < 0 
        γ_sam[(p + 2), :, l + 1] = -γ_sam[(p + 2), :, l + 1]
    end
    if γ_sam[p + 3, 1, l + 1] < 0 
        γ_sam[(p + 3), :, l + 1] = -γ_sam[(p + 3), :, l + 1]
    end
    next!(prog) # monitor the progress
end
Computing initial pass...100%|██████████████████████████████████████████████████| Time: 1:49:04
In [53]:
β_pos_sam = Array{Float64, 3}(undef, N_sam + 1, p * q, 1);
β_pos_sam[:, :, 1] = hcat(γ_sam[1, 1, :], γ_sam[1, 2, :], γ_sam[2, 1, :], γ_sam[2, 2, :]);
β_chain = Chains(β_pos_sam);
 = plot(β_chain)
Out[53]:
0 5.0×10 3 1.0×10 4 1.5×10 4 2.0×10 4 2.5×10 4 0.5 1.0 1.5 2.0 Param1 Iteration Sample value 0.0 0.5 1.0 1.5 2.0 0.0 0.5 1.0 1.5 2.0 Param1 Sample value Density 0 5.0×10 3 1.0×10 4 1.5×10 4 2.0×10 4 2.5×10 4 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 Param2 Iteration Sample value -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 0.0 0.5 1.0 1.5 Param2 Sample value Density 0 5.0×10 3 1.0×10 4 1.5×10 4 2.0×10 4 2.5×10 4 -5 -4 -3 -2 Param3 Iteration Sample value -5 -4 -3 -2 -1 0.00 0.25 0.50 0.75 1.00 Param3 Sample value Density 0 5.0×10 3 1.0×10 4 1.5×10 4 2.0×10 4 2.5×10 4 -2 -1 0 1 2 3 Param4 Iteration Sample value -2 -1 0 1 2 3 0.0 0.2 0.4 0.6 0.8 Param4 Sample value Density
In [54]:
Λ_pos_sam = Array{Float64, 3}(undef, N_sam + 1, K * q, 1);
Λ_pos_sam[:, :, 1] = hcat(γ_sam[3, 1, :], γ_sam[3, 2, :], γ_sam[4, 1, :], γ_sam[4, 2, :], 
    γ_sam[5, 1, :], γ_sam[5, 2, :]);
Λ_chain = Chains(Λ_pos_sam);
 = plot(Λ_chain)
Out[54]:
0 5.0×10 3 1.0×10 4 1.5×10 4 2.0×10 4 2.5×10 4 0 2 4 6 Param1 Iteration Sample value 0 2 4 6 0.0 0.1 0.2 0.3 0.4 Param1 Sample value Density 0 5.0×10 3 1.0×10 4 1.5×10 4 2.0×10 4 2.5×10 4 -10.0 -7.5 -5.0 -2.5 0.0 Param2 Iteration Sample value -10.0 -7.5 -5.0 -2.5 0.0 0.00 0.05 0.10 0.15 0.20 0.25 Param2 Sample value Density 0 5.0×10 3 1.0×10 4 1.5×10 4 2.0×10 4 2.5×10 4 0 1 2 3 Param3 Iteration Sample value 0 1 2 3 4 0.0 0.1 0.2 0.3 0.4 0.5 0.6 Param3 Sample value Density 0 5.0×10 3 1.0×10 4 1.5×10 4 2.0×10 4 2.5×10 4 -5 -4 -3 -2 -1 0 1 Param4 Iteration Sample value -5 -4 -3 -2 -1 0 1 0.0 0.1 0.2 0.3 0.4 Param4 Sample value Density 0 5.0×10 3 1.0×10 4 1.5×10 4 2.0×10 4 2.5×10 4 0 1 2 3 Param5 Iteration Sample value 0 1 2 3 0.0 0.2 0.4 0.6 0.8 Param5 Sample value Density 0 5.0×10 3 1.0×10 4 1.5×10 4 2.0×10 4 2.5×10 4 -4 -3 -2 -1 0 1 Param6 Iteration Sample value -4 -3 -2 -1 0 1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 Param6 Sample value Density
In [55]:
Σ_pos_sam = Array{Float64, 3}(undef, N_sam + 1, q * q, 1);
Σ_pos_sam[:, :, 1] = hcat(Σ_sam[1, 1, :], Σ_sam[1, 2, :], Σ_sam[2, 1, :], Σ_sam[2, 2, :]);
Σ_chain = Chains(Σ_pos_sam);
 = plot(Σ_chain)
Out[55]:
0 5.0×10 3 1.0×10 4 1.5×10 4 2.0×10 4 2.5×10 4 1 2 3 4 5 Param1 Iteration Sample value 1 2 3 4 5 0.0 0.5 1.0 1.5 Param1 Sample value Density 0 5.0×10 3 1.0×10 4 1.5×10 4 2.0×10 4 2.5×10 4 -5 -4 -3 -2 -1 0 Param2 Iteration Sample value -5 -4 -3 -2 -1 0 0.0 0.2 0.4 0.6 0.8 1.0 1.2 Param2 Sample value Density 0 5.0×10 3 1.0×10 4 1.5×10 4 2.0×10 4 2.5×10 4 -5 -4 -3 -2 -1 0 Param3 Iteration Sample value -5 -4 -3 -2 -1 0 0.0 0.2 0.4 0.6 0.8 1.0 1.2 Param3 Sample value Density 0 5.0×10 3 1.0×10 4 1.5×10 4 2.0×10 4 2.5×10 4 1 2 3 4 5 6 Param4 Iteration Sample value 1 2 3 4 5 6 0.0 0.2 0.4 0.6 0.8 Param4 Sample value Density
In [15]:
?lsmr!
search: lsmr! lsmr MLEstimator WalleniusNoncentralHypergeometric

Out[15]:
lsmr!(x, A, b; kwargs...) -> x, [history]

Minimizes $\|Ax - b\|^2 + \|λx\|^2$ in the Euclidean norm. If multiple solutions exists the minimum norm solution is returned.

The method is based on the Golub-Kahan bidiagonalization process. It is algebraically equivalent to applying MINRES to the normal equations $(A^*A + λ^2I)x = A^*b$, but has better numerical properties, especially if $A$ is ill-conditioned.

Arguments

  • x: Initial guess, will be updated in-place;
  • A: linear operator;
  • b: right-hand side.

Keywords

  • λ::Number = 0: lambda.
  • atol::Number = 1e-6, btol::Number = 1e-6: stopping tolerances. If both are 1.0e-9 (say), the final residual norm should be accurate to about 9 digits. (The final x will usually have fewer correct digits, depending on cond(A) and the size of damp).
  • conlim::Number = 1e8: stopping tolerance. lsmr terminates if an estimate of cond(A) exceeds conlim. For compatible systems Ax = b, conlim could be as large as 1.0e+12 (say). For least-squares problems, conlim should be less than 1.0e+8. Maximum precision can be obtained by setting
  • atol = btol = conlim = zero, but the number of iterations may then be excessive.
  • maxiter::Int = maximum(size(A)): maximum number of iterations.
  • log::Bool: keep track of the residual norm in each iteration;
  • verbose::Bool: print convergence information during the iterations.

Return values

if log is false

  • x: approximated solution.

if log is true

  • x: approximated solution.
  • ch: convergence history.

ConvergenceHistory keys

  • :atol => ::Real: atol stopping tolerance.
  • :btol => ::Real: btol stopping tolerance.
  • :ctol => ::Real: ctol stopping tolerance.
  • :anorm => ::Real: anorm.
  • :rnorm => ::Real: rnorm.
  • :cnorm => ::Real: cnorm.
  • :resnom => ::Vector: residual norm at each iteration.